package apps.sim.proxy;

import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;

public class SocketAddress {
	final String hostName;
	final int hostPort;
	final byte[] request;
	final int requestLength;

	public SocketAddress(String hostName, int hostPort, byte[] request) {
		this.hostName = hostName;
		this.hostPort = hostPort;
		this.request = request;
		this.requestLength = Array.getLength(request);
	}

	public SocketAddress(String oldUrl, String newUrl, byte[] request) throws MalformedURLException {
		final URL newUrlObject = new URL(newUrl);
		final URL oldUrlObject = new URL(oldUrl);
		this.hostName = newUrlObject.getHost();
		this.hostPort = newUrlObject.getPort();
		final String string = new String(request);
		final String replaceFirst = string.replaceFirst(oldUrl, newUrl);
		final String newRequest = replaceFirst.replaceFirst(oldUrlObject.getHost() + ":" + oldUrlObject.getPort(), newUrlObject.getHost() + ":"
				+ newUrlObject.getPort());
		this.request = newRequest.getBytes();
		this.requestLength = Array.getLength(newRequest.getBytes());
	}

	public String getAddress() {
		return this.hostName + ":" + this.hostPort;

	}
}